package cn.hp.controller;
import cn.hp.entity.User;
import cn.hp.service.UserService;
import cn.hp.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping
    public Result find(User user){
        List<User> list = userService.selectUser(user);
        return Result.success(list);
    }
    @RequestMapping("/page")
    public Result page(User user, @RequestParam(value = "page", defaultValue = "1") Integer page,@RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        PageInfo<User> pageInfo = userService.selectUser(user, page, limit);
        return Result.success(pageInfo);
    }
    @PostMapping
    public Result add(@RequestBody User user) {
        int i = userService.insertUser(user);
        return i > 0 ? Result.success("新增成功") : Result.error("新增失败");
    }
    @PutMapping
    public Result update(@RequestBody User user){
        int i = userService.updateUser(user);
        return i > 0 ? Result.success("修改成功") : Result.error("修改失败");
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        int i = userService.deleteUserById(id);
        return i > 0 ? Result.success("删除成功") : Result.error("删除失败");
    }
    @GetMapping("/resetPassword/{id}")
    public Result resetPassword(@PathVariable Integer id) {
        User user2 = new User();
        user2.setId(id);
        user2.setPlainPassword("123");
        int i = userService.resetPassword(user2);
        return i > 0 ? Result.success("重置成功") : Result.error("重置失败");
    }

}